package mudclient;

import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class MudClientTextPane extends JTextPane {
	
	public MudClientTextPane(JScrollBar blah) { 
		super();
		mybar = blah;
		sd = this.getStyledDocument();
		currentStyle = sd.addStyle("default", null);
		StyleConstants.setFontFamily(currentStyle, "Fixedsys");
		JOptionPane.showMessageDialog(MudClientApplication.getMainFrame(),
				StyleConstants.getFontFamily(currentStyle));
		StyleConstants.setForeground( currentStyle, new Color(153, 153, 153) );
		StyleConstants.setBackground( currentStyle, Color.black );
		Style defaultOutputStyle = sd.addStyle("defaultOutput", null);
		StyleConstants.setForeground( defaultOutputStyle, Color.yellow);
		StyleConstants.setBackground( defaultOutputStyle, Color.black );
	}
	
	public void setReaderThread(TelnetThread rt) {
		this.rt = rt;
	}
	public TelnetThread getReaderThread() {
		return rt;
	}

	public void setAnsiStyle(int ansiCode) {
		if (ansiCode == 0) {
			javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	            	StyleConstants.setForeground( currentStyle, new Color(153, 153, 153));
	    			StyleConstants.setBackground( currentStyle, Color.black);
	    			bold = false;
	            }
	        });
		} else if (ansiCode == 1) {
			javax.swing.SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	    			bold = true;
	            	StyleConstants.setForeground( currentStyle, getColor(lastColorCode));
	            }
	        });
		} else if (ansiCode > 29 && ansiCode < 50){
			if(ansiCode < 40) {
				lastColorCode = ansiCode;
				final int c = ansiCode;
				javax.swing.SwingUtilities.invokeLater(new Runnable() {
		            public void run() {
		            	StyleConstants.setForeground( currentStyle, getColor(c));
		            }
		        });
			} else if(ansiCode > 39) {
				final int c = ansiCode;
				javax.swing.SwingUtilities.invokeLater(new Runnable() {
		            public void run() {
		            	StyleConstants.setBackground( currentStyle, getColor(c));
		            }
		        });
			}
		}
	}
	
	public void appendString(String text) {
    	appendString(text, currentStyle);
	}
	
	public void appendString(String text, Style temp) {
		final String t1 = text;
		final Style t = temp;
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	try {
            		sd.insertString(sd.getLength(), t1, t);
            	} catch(BadLocationException e) {
            		
            	}
            	javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                    	if(!mybar.getValueIsAdjusting())
                    		mybar.setValue(mybar.getMaximum());
                    }
                });
            }
        });
	}
	
	public void appendString(String text, String temp) {
    	appendString(text, sd.getStyle(temp));
	}
	
	private Color getColor(int lc) {
		if(bold == true) {
			switch (lc) {
				case 30: case 40:
					return new Color(153, 153, 153);
				case 31: case 41:
					return new Color(255, 0, 0);
				case 32: case 42:
					return new Color(0, 255, 0);
				case 33: case 43:
					return new Color(255, 255, 0);
				case 34: case 44:
					return new Color(0, 0, 255);
				case 35: case 45:
					return new Color(255, 0, 255);
				case 36: case 46:
					return new Color(0, 255, 255);
				case 37: case 47:
					return new Color(255, 255, 255);
			}
		} else {
			switch (lc) {
			case 30: case 40:
				return new Color(0, 0, 0);
			case 31: case 41:
				return new Color(153, 0, 0);
			case 32: case 42:
				return new Color(0, 153, 0);
			case 33: case 43:
				return new Color(153, 153, 0);
			case 34: case 44:
				return new Color(0, 0, 153);
			case 35: case 45:
				return new Color(153, 0, 153);
			case 36: case 46:
				return new Color(0, 153, 153);
			case 37: case 47:
				return new Color(153, 153, 153);
		}
	
		}
		return null;
	}
	
	private boolean bold = false;
	private int lastColorCode = 0;
	private Style currentStyle;
	private StyledDocument sd;
	private TelnetThread rt;
	private JScrollBar mybar;

}
